package com.gurid.service.vod.controller.admin;

import com.gurid.common.base.result.R;
import com.gurid.common.base.result.ResultCode;
import com.gurid.common.base.util.ExceptionUtils;
import com.gurid.service.base.exception.GuridException;
import com.gurid.service.vod.service.VideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Tag(name = "MediaController", description = "阿里云视频点播")
//@CrossOrigin
@RestController
@RequestMapping("/admin/vod/media")
@Slf4j
public class MediaController {

    @Autowired
    private VideoService videoService;

    @PostMapping("upload")
    public R uploadVideo(
            @Parameter(name = "file", description = "上传的文件", required = true)
            @RequestBody(
                    content = @Content(
                            mediaType = "multipart/form-data",
                            schema = @Schema(type = "object"),
                            schemaProperties = {
                                    @SchemaProperty(name = "file", schema = @Schema(type = "string", format = "binary"))
                            }))/*@RequestParam("file")不管用*/ MultipartFile file) {

        try {
            String originalFilename = file.getOriginalFilename();
            System.out.println("------------------" + originalFilename+"----开始上传");
            InputStream inputStream = file.getInputStream();
            String videoId = videoService.uploadVideo(inputStream, originalFilename);
            //String videoId = videoService.uploadVideo(originalFilename);

            return R.ok().message("上传成功").data("videoId",videoId);
        } catch (IOException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw  new GuridException(ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
    }

    @DeleteMapping("remove/{videoId}")
    public R removeVideo(
            @Parameter(name = "videoId", description ="阿里云视频id", required = true)
            @PathVariable String videoId) {
        try {
            videoService.removeVideo(videoId);
            return R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuridException(ResultCode.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }

    @DeleteMapping("remove")
    public R removeVideoByIdList(
            @Parameter(description = "阿里云视频id列表", required = true)
            @org.springframework.web.bind.annotation.RequestBody List<String> videoIdList) {
        try {
            videoService.removeVideoByIdList(videoIdList);
            return R.ok().message("文件删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuridException(ResultCode.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}
